package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.ChannelGroupJurisdictionAddDTO;
import org.jsola.hr.dto.ChannelGroupJurisdictionUpdateDTO;
import org.jsola.hr.query.ChannelGroupJurisdictionQuery;
import org.jsola.hr.service.IChannelGroupJurisdictionService;
import org.jsola.hr.vo.ChannelGroupJurisdictionListVO;
import org.jsola.hr.vo.ChannelGroupJurisdictionVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 渠道分组权限表
 *
 * @author zhr
 */
@RestController("hrChannelGroupJurisdictionController")
@RequestMapping(HrConstants.API_PATH + "/v1/channelGroupJurisdictions")
public class ChannelGroupJurisdictionController implements IChannelGroupJurisdictionControllerAPI {

    @Autowired
    private IChannelGroupJurisdictionService channelGroupJurisdictionService;

    @Override
    @PostMapping("/batchSave")
    public Result<Integer> batchSave(@Valid @RequestBody ChannelGroupJurisdictionAddDTO channelGroupJurisdictionAddDTO) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                channelGroupJurisdictionService.batchSave(channelGroupJurisdictionAddDTO, tokenUser)
        );
    }

    @Override
    @PostMapping
    public Result<ChannelGroupJurisdictionVO> save(@Valid @RequestBody ChannelGroupJurisdictionAddDTO channelGroupJurisdictionAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增渠道分组权限表权限校验

        return Result.success(
                channelGroupJurisdictionService.save(channelGroupJurisdictionAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody ChannelGroupJurisdictionUpdateDTO channelGroupJurisdictionUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改渠道分组权限表权限校验

        int count = channelGroupJurisdictionService.update(channelGroupJurisdictionUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{channelGroupJurisdictionId}")
    public Result<Integer> delete(@PathVariable String channelGroupJurisdictionId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除渠道分组权限表权限校验

        int count = channelGroupJurisdictionService.deleteByIds(tokenUser, channelGroupJurisdictionId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] channelGroupJurisdictionIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除渠道分组权限表权限校验

        int count = channelGroupJurisdictionService.deleteByIds(tokenUser, channelGroupJurisdictionIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{channelGroupJurisdictionId}")
    public Result<ChannelGroupJurisdictionVO> findById(@PathVariable String channelGroupJurisdictionId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看渠道分组权限表权限校验

        return Result.success(
            channelGroupJurisdictionService.selectById(channelGroupJurisdictionId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<ChannelGroupJurisdictionListVO>> listAll(@Valid ChannelGroupJurisdictionQuery channelGroupJurisdictionQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看渠道分组权限表权限校验

        return Result.success(
            channelGroupJurisdictionService.select(channelGroupJurisdictionQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<ChannelGroupJurisdictionListVO>> page(@Valid ChannelGroupJurisdictionQuery channelGroupJurisdictionQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            channelGroupJurisdictionService.selectPage(channelGroupJurisdictionQuery, tokenUser.getSiteId())
        );
    }


}

